package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.TeacherReqVO;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.view.TeacherInfoView;

/**
 * 教师管理控制器
 * @author huqimeng
 * @date 2017年5月10日
 */

@RequestMapping(Const.TEACHER_ROLE + MenuConst.TEACHER_MANAGE)
@Controller
public class TeacherManageController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(TeacherManageController.class);
	
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping(value={"","/"})
	public String teacherList() {
		return "teacher/teacherList";
	}

	/**
	 * 分页查询教师列表
	 * @param vo
	 * @return
	 */
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(TeacherReqVO vo) {
		try {
			Page<TeacherInfo> page = new Page<TeacherInfo>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("姓名"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<TeacherInfo> list = teacherService.selectByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("keyword")
	@ResponseBody Object keywordQuery(TeacherReqVO reqVO) {
		AppUser appUser = getAppUser();
		reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
		List<TeacherInfo> list = teacherService.selectByKeyword(reqVO);
		return list;
	}
	
	@RequestMapping("mailList")
	@ResponseBody Object mailList(TeacherReqVO reqVO) {
		AppUser appUser = getAppUser();
		reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
		List<TeacherInfo> list = teacherService.selectForChatByParam(reqVO);
		List<TeacherInfoView> viewList = BeanUtils.copyAs(list, TeacherInfoView.class);
		return viewList;
	}
	
	/**
	 * 根据教师id查询教师信息
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("info/query/{teacherId}")
	public String infoQuery(@PathVariable Integer teacherId, Model model) {
		TeacherInfo teacherInfo = teacherService.selectById(teacherId);
		TeacherInfoView infoView = BeanUtils.copyAs(teacherInfo, TeacherInfoView.class);
		model.addAttribute("teacher", infoView);
		return "teacher/teacherDetail";
	}
	
	/**
	 * 教师信息修改
	 * @param record
	 * @return
	 */
	@RequestMapping("info/update")
	@ResponseBody Object infoUpdate(TeacherInfo record){
		try {
			teacherService.updateById(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----infoUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
